/*
 * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.example.test.mo1;

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.mlsdk.model.download.MLLocalModelManager;
import com.huawei.hms.mlsdk.model.download.MLModelDownloadListener;
import com.huawei.hms.mlsdk.model.download.MLModelDownloadStrategy;
import com.huawei.hms.mlsdk.translate.MLTranslatorFactory;
import com.huawei.hms.mlsdk.translate.local.MLLocalTranslateSetting;
import com.huawei.hms.mlsdk.translate.local.MLLocalTranslator;
import com.huawei.hms.mlsdk.translate.local.MLLocalTranslatorModel;

import androidx.appcompat.app.AppCompatActivity;

public class LanguageTranslation extends AppCompatActivity {

    private MLModelDownloadListener modelDownloadListener;

    private MLModelDownloadStrategy downloadStrategy;

    private void initLocalModelManagement() {
        // Listening to the download progress.
        modelDownloadListener = new MLModelDownloadListener() {
            @Override
            public void onProcess(long alreadyDownLength, long totalLength) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                    }
                });
            }
        };

        // Download the offline model required for local offline translation.
        downloadStrategy = new MLModelDownloadStrategy.Factory()
                // It is recommended that you download the package in a Wi-Fi environment.
                .needWifi()
                .create();
    }

    /**
     * Text translation on the device.
     */
    private void localTranslator(final String input) {
        // Create an offline translator.
        MLLocalTranslateSetting setting = new MLLocalTranslateSetting
                .Factory()
                // Set the source language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
                .setSourceLangCode("en")
                // Set the target language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur.
                .setTargetLangCode("zh")
                .create();
        final MLLocalTranslator localTranslator = MLTranslatorFactory.getInstance().getLocalTranslator(setting);

        localTranslator.preparedModel(downloadStrategy, modelDownloadListener)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        // Called when the model package is successfully downloaded.
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
            }
        });
        final Task<String> task = localTranslator.asyncTranslate(input);
        task.addOnSuccessListener(new OnSuccessListener<String>() {
            @Override
            public void onSuccess(String text) {
                // Recognition success.
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                // Recognition failure.
            }
        });
    }

    private void releaseModel() {
        MLLocalModelManager manager = MLLocalModelManager.getInstance();
        MLLocalTranslatorModel model = new MLLocalTranslatorModel.Factory("zh").create();
        manager.deleteModel(model).addOnSuccessListener(new OnSuccessListener<Void>() {
            public void onSuccess(Void aVoid) {
                // Processing logic for recognition success.
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                // Processing logic for detection failure.
            }
        });
    }

    private void stop(MLLocalTranslator localTranslator) {
        if (localTranslator != null) {
            localTranslator.stop();
        }
    }
}
